//
//  MTLuckyResultViewController.swift
//  MT
//
//  Created by ooops on 2020/1/21.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc protocol MTLuckyResultViewControllerDelegate: NSObjectProtocol {
    /// 继续抽奖
    @objc optional func runAgain()
}

class MTLuckyResultViewController: BaseViewController {
    
    private var luckyMoney: String = ""
    
    weak var delegate: MTLuckyResultViewControllerDelegate?
    
    lazy var closeButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("关闭", for: .normal)
        btn.addTarget(self, action: #selector(close), for: .touchUpInside)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(18)
        btn.layer.borderColor = UIColor.white.cgColor
        btn.layer.borderWidth = 0.5
        btn.backgroundColor = UIColor(hex: 0x000000, alpha: 0.5)
        btn.layer.cornerRadius = 24
        btn.layer.masksToBounds = true
        return btn
    }()
    
    lazy var againButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("继续抽奖", for: .normal)
        btn.addTarget(self, action: #selector(again), for: .touchUpInside)
        btn.setTitleColor(UIColor.text.x161616, for: .normal)
        btn.titleLabel?.font = PingFangSCMediumFont(18)
        btn.backgroundColor = UIColor.theme.main
        btn.layer.cornerRadius = 24
        btn.layer.masksToBounds = true
        return btn
    }()
    
    lazy var bottomTipLael: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.theme.main
        l.text = "点券已自动放入您的账户"
        l.textAlignment = .center
        return l
    }()
    
    lazy var tipImageView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "zhongjiang-png"))
        return imageV
    }()
    
    lazy var moneyLabel: UILabel = {
        let l = UILabel()
        //l.text = "恭喜您!抽中\n99999点券"
        l.numberOfLines = 2
        return l
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0x000000, alpha: 0.7)
        setupUI()
        makeConstraint()
    }
    
    private func setupUI() {
        
        view.addSubview(closeButton)
        view.addSubview(againButton)
        
        view.addSubview(bottomTipLael)
        view.addSubview(tipImageView)
        
        view.addSubview(moneyLabel)
        
    }
    
    private func makeConstraint() {
        
        moneyLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(tipImageView.snp.top).offset(-40)
        }
        
        tipImageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(bottomTipLael.snp.top).offset(-4)
        }
        
        bottomTipLael.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(closeButton.snp.top).offset(-30)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-TabBarHeight - 40)
            make.right.equalTo(view.snp.centerX).offset(-18)
            make.width.equalTo(128)
            make.height.equalTo(48)
        }
        
        againButton.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-TabBarHeight - 40)
            make.left.equalTo(view.snp.centerX).offset(18)
            make.width.equalTo(128)
            make.height.equalTo(48)
        }
    }

    convenience init(_ luckyMoney: String) {
        self.init()
        self.luckyMoney = luckyMoney
        
        //l.text = "恭喜您!抽中\n99999点券"
        
        let paraStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
        paraStyle.alignment = .center
        
        let attributeAll = [NSAttributedString.Key.font: PingFangSCRegularFont(18),
                                    NSAttributedString.Key.foregroundColor: UIColor(hex: 0xFFC000),
                                    NSAttributedString.Key.paragraphStyle : paraStyle]
        
        let arrtibuteString: NSMutableAttributedString = NSMutableAttributedString(string: "恭喜您! 抽中\n\(luckyMoney)点券", attributes: attributeAll)
        
        let attribute = [NSAttributedString.Key.font: PingFangSCSemiboldFont(50),
                         NSAttributedString.Key.foregroundColor: UIColor(hex: 0xFFC000),
                         NSAttributedString.Key.paragraphStyle : paraStyle]
        
        
        arrtibuteString.addAttributes(attribute, range: "恭喜您! 抽中\n\(luckyMoney)点券".nsrange(of: "\(luckyMoney)")[0])
        
        moneyLabel.attributedText = arrtibuteString
    }
    
    
    @objc private func close() {
        self.dismiss(animated: true) {
            
        }
    }
    
    @objc private func again() {
        self.dismiss(animated: true) {
            self.delegate?.runAgain?()
        }
    }
    
}
